package com.zx.chatroom.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zx.chatroom.po.Dept;
import com.zx.chatroom.po.Role;
import com.zx.framework.core.dao.IBaseDAO;
import com.zx.framework.core.exception.ServiceException;
import com.zx.framework.core.util.StringUtil;

@Component("roleService")
@Transactional
public class RoleService {

	private static final Logger LOGGER = Logger.getLogger(RoleService.class);
	@Resource
	private IBaseDAO baseDAO;
	
	/**
	 * 获取role详细信息
	 * @param id
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Role findRoleById(String id){
		if(StringUtil.isEmpty(id)){
			ServiceException exception = new ServiceException("Role ID must be not null!");
			LOGGER.error(exception.getMessage());
			throw exception;
		}
		return baseDAO.findById(id, Role.class);
	}
	
	/**
	 * 获取用户的所有角色信息
	 * 
	 * @param userid
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<Role> findRolesByUserId(String userid) {
		String hql = "from Role r where exists(from UserRole u where r.id = u.id.roleId and u.id.userId=?) order by r.layer desc";
		return baseDAO.queryHQL(hql, userid);
	}
}
